package view;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * History Frame
 * 
 * Class for a Frame for a History of Game Events
 * creates a Window with the History
 * with a Button "OK" to close the Window
 * @author Thomas
 * @author wsiebert
 * @since 
 */

public class HistoryFrame extends JDialog
{
	private JTextArea history;
	private JScrollPane historyScroll;
	private JButton ok;
	private JPanel historyPanel;
	
	private final static String newline = "\n";
	private final static String lineSeparator = System.getProperty ( "line.separator" );
	
    private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }
	
	public HistoryFrame() 
	{
		historyPanel = new JPanel();
		historyPanel.setLayout(new FlowLayout());
		
		history = new JTextArea(12, 40);
		history.setLineWrap(true);
		history.setEditable(false);
		history.setWrapStyleWord( true );
		
		this.setTitle("Spielverlauf");
		
		ok = new JButton("OK");
		ok.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ev)
			{
				setHistoryInvisible();
			}
		});
		
		historyScroll = new JScrollPane(history);
		historyScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		historyScroll.setPreferredSize(new Dimension(522,204));
		historyPanel.add(ok);
		historyPanel.add(historyScroll);

		//historyPanel.setPreferredSize(new Dimension(522,204));
		this.getContentPane().add(historyPanel);
		this.pack();
		this.setResizable(true);
		this.setLocation(200, 150);
		this.setAlwaysOnTop(true);
		this.setModal(true);
	}
	
	/**
	 * Method to add a new Entry in the History 
	 * @param logEvent new Entry
	 */
	public void addHistoryEntry(String logEvent) {
		
		this.history.append(getDateTime()+ "  " + logEvent + newline + lineSeparator);
	}
	
	/**
	 * Method to set the HistoryWindow visible
	 */
	public void setHistoryVisible(){
		this.setVisible(true);
		repaint();
	}
	
	/**
	 * Method to set the HistoryWindow invisible
	 */
	public void setHistoryInvisible(){
		this.setVisible(false);
		
	}
}
